<?php
namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\VirtualOrderInsertDTO;
use App\Exceptions\ServiceException;
use App\Models\Game\Equip;
use App\Models\Game\PackageRecord;
use App\Models\Rewards;
use App\Services\Game\PackageService;
use App\Services\Game\UserRoleService;
use App\Services\VirtualOrderService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use App\Utils\ResponseUtil;
use App\DTOS\Admin\RewardsSearchDTO;
use App\Services\RewardsService;
// 奖励
class RewardsController extends Controller
{
    private RewardsService $service;
    private PackageService $packageService;
    private VirtualOrderService $virtualOrderService;
    private UserRoleService $userRoleService;

    /**
     * RewardsService constructor.
     * @param RewardsService $service
     * @param PackageService $packageService
     * @param VirtualOrderService $virtualOrderService
     * @param UserRoleService $userRoleService
     */
    public function __construct(RewardsService $service,PackageService $packageService,VirtualOrderService $virtualOrderService,UserRoleService $userRoleService)
    {
        $this->service = $service;
        $this->packageService = $packageService;
        $this->virtualOrderService = $virtualOrderService;
        $this->userRoleService = $userRoleService;
    }

    public function import()
    {
        $file = request()->file('file');
        $type = request()->get('type',1);
        $sheets = Excel::toArray(null,$file);
        $sheet = $sheets[0];
        $inserts = [];
        foreach($sheet as $key=>$arr) {
            if ($key>0 && !empty($arr[2])){
                $virEquipsText = $arr[8];
                $equipsText = $arr[9];
                $equips = [];
                $virEquipList = [];
                if (!empty($virEquipsText)){
                    $array = explode('|',$virEquipsText);
                    foreach($array as $item){
                        $itemArr = explode(':',$item);
                        $virEquipList[] = [
                            'equip_id'=>$itemArr[0],
                            'num'=>$itemArr[1],
                            'bind'=>0,
                        ];
                    }
                }

                if (!empty($equipsText)){
                    $equipArr = explode('|',$equipsText);
                    foreach($equipArr as $item){
                        $itemArr = explode(':',$item);
                        $equips[] = [
                            'equip_id'=>$itemArr[0],
                            'num'=>$itemArr[1],
                            'bind'=>$itemArr[2],
                        ];
                    }
                }

                $row = [
                    'type'=>$type,
                    'sort'=>$arr[0],
                    'server_id'=>$arr[1],
                    'customer_id'=>$arr[2],
                    'yuanbao'=>$arr[3],
                    'hxb'=>$arr[4],
                    'yinyuan'=>$arr[5],
                    'tongbi'=>$arr[6],
                    'payjinbi'=>$arr[7],
                    'vir_equips'=>json_encode($virEquipList),
                    'equips'=>json_encode($equips),
                    'title'=>$arr[10],
                    'content'=>$arr[11],
                    'begin_time'=>date("Y-m-d H:i:s",$arr[12]),
                    'end_time'=>date("Y-m-d H:i:s",$arr[13]),
                ];
                foreach($row as $key=>$val){
                    if (!$val) $val = null;
                    $row[$key] = $val;
                }
                $inserts[] = $row;
            }
        }

        Rewards::query()->insert($inserts);

        return ResponseUtil::success();
    }

    public function paginate()
    {
        $DTO = new RewardsSearchDTO(request()->all());
        $paginate = $this->service->paginate($DTO);
        $data = $paginate->items();

        foreach ($data as &$datum){
            $datum['customer_id'] = (string)$datum['customer_id'];
        }

        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

    public function delete($id){
        try{
            $this->service->deleteById($id);
            return ResponseUtil::success();
        } catch (\Exception $e){
            return ResponseUtil::error($e->getMessage());
        }
    }

    public function detail($id)
    {
        $data = $this->service->getById($id);
        return ResponseUtil::success($data);
    }

}
